package com.sise.wangzhan.client;

import com.netflix.config.ConfigurationManager;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.sise.wangzhan.command.MyHystrixCollapser;

import java.time.Period;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @Classname CollapserCommand
 * @Description TODO    测试 合并请求 类
 * @Date 2020/4/29 20:27
 * @Created by wangzhan
 */
public class CollapserCommand {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 收集1秒内发生的请求，合并为一个命令执行
        ConfigurationManager.getConfigInstance().setProperty(
                "hystrix.collapser.default.timerDelayImMilliseconds", 1000
        );
        ConfigurationManager.getConfigInstance().setProperty(
                "hystrix.command.default.execution.timeout.enabled", false
        );

        //请求上下文
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        //创建请求合并处理器
        MyHystrixCollapser c1 = new MyHystrixCollapser("Angus");
        MyHystrixCollapser c2 = new MyHystrixCollapser("Crazyit");
        MyHystrixCollapser c3 = new MyHystrixCollapser("Wang");
        MyHystrixCollapser c4 = new MyHystrixCollapser("Zhan");
        MyHystrixCollapser c5 = new MyHystrixCollapser();

        //异步执行
        Future<com.sise.wangzhan.command.CollapserCommand.Person> f1 = c1.queue();
        Future<com.sise.wangzhan.command.CollapserCommand.Person> f2 = c2.queue();
        Future<com.sise.wangzhan.command.CollapserCommand.Person> f3 = c3.queue();
        Future<com.sise.wangzhan.command.CollapserCommand.Person> f4 = c4.queue();
        Future<com.sise.wangzhan.command.CollapserCommand.Person> f5 = c5.queue();

        System.out.println(f1.get());
        System.out.println(f2.get());
        System.out.println(f3.get());
        System.out.println(f4.get());
        System.out.println(f5.get());

        context.shutdown();
    }
}
